package com.atguigu.tingshu.payment.service.impl;

import com.atguigu.tingshu.account.client.RechargeInfoFeignClient;
import com.atguigu.tingshu.common.constant.KafkaConstant;
import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.service.KafkaService;
import com.atguigu.tingshu.model.account.RechargeInfo;
import com.atguigu.tingshu.model.order.OrderInfo;
import com.atguigu.tingshu.model.payment.PaymentInfo;
import com.atguigu.tingshu.order.client.OrderInfoFeignClient;
import com.atguigu.tingshu.payment.mapper.PaymentInfoMapper;
import com.atguigu.tingshu.payment.service.PaymentInfoService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wechat.pay.java.service.payments.model.Transaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.math.BigDecimal;
import java.util.Date;

@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class PaymentInfoServiceImpl extends ServiceImpl<PaymentInfoMapper, PaymentInfo> implements PaymentInfoService {

    @Autowired
    private RechargeInfoFeignClient rechargeInfoFeignClient;

    @Autowired
    private KafkaService kafkaService;

    @Autowired
    private OrderInfoFeignClient orderInfoFeignClient;

    @Override
    public void savePaymentInfo(String orderNo, String paymentType) {
        //  保存交易记录.
        //  添加数据：
        //  创建对象
        PaymentInfo paymentInfo = new PaymentInfo();
        paymentInfo.setPaymentType(paymentType);
        //  只有订单的金额：
        //  paymentInfo.setAmount(orderInfo.getOrderAmount());
        if (paymentType.equals(SystemConstant.PAYMENT_TYPE_RECHARGE)){
            //  获取充值记录
            Result<RechargeInfo> rechargeInfoResult = rechargeInfoFeignClient.getRechargeInfo(orderNo);
            Assert.notNull(rechargeInfoResult,"充值结果集为空");
            RechargeInfo rechargeInfo = rechargeInfoResult.getData();
            Assert.notNull(rechargeInfo,"充值对象为空");
            paymentInfo.setAmount(rechargeInfo.getRechargeAmount());
            paymentInfo.setPayWay(rechargeInfo.getPayWay());
            paymentInfo.setUserId(rechargeInfo.getUserId());

        } else {
            //  订单：
            Result<OrderInfo> orderInfoResult = orderInfoFeignClient.getOrderInfo(orderNo);
            Assert.notNull(orderInfoResult,"返回订单结果集为空");
            OrderInfo orderInfo = orderInfoResult.getData();
            Assert.notNull(orderInfo,"订单对象为空");
            paymentInfo.setPayWay(orderInfo.getPayWay());
            paymentInfo.setUserId(orderInfo.getUserId());
            //  赋值：
            //  赋值交易记录金额
            paymentInfo.setAmount(orderInfo.getOrderAmount());
        }
        //  赋值订单编号
        paymentInfo.setOrderNo(orderNo);
        //  微信或支付宝的交易号
        //  paymentInfo.setOutTradeNo();
        String content = paymentType.equals(SystemConstant.PAYMENT_TYPE_ORDER)?"订单支付":"充值支付";
        paymentInfo.setContent(content);
        paymentInfo.setPaymentStatus(SystemConstant.PAYMENT_STATUS_UNPAID);
        //  保存交易记录.
        this.save(paymentInfo);
    }

    @Override
    public void updatePaymentStatus(Transaction transaction) {
        //  先查询当前交易记录表中是否有交易记录. 点击支付的时候就会保存交易记录信息.
        //  创建条件
        LambdaQueryWrapper<PaymentInfo> wrapper = new LambdaQueryWrapper<>();
        //  微信系统中商户的订单号为outTradeNo; orderNo;
        wrapper.eq(PaymentInfo::getOrderNo,transaction.getOutTradeNo());
        PaymentInfo paymentInfo = this.getOne(wrapper);
        //  判断
        if (null == paymentInfo){
            return;
        }
        //  根据订单号更新交易状态。
        //  PaymentInfo paymentInfo = new PaymentInfo();
        //  微信支付系统的订单号
        paymentInfo.setOutTradeNo(transaction.getTransactionId());
        paymentInfo.setPaymentStatus(SystemConstant.PAYMENT_STATUS_PAID);
        paymentInfo.setCallbackTime(new Date());
        paymentInfo.setContent(transaction.toString());
        //  根据Id 更新数据
        this.updateById(paymentInfo);
        //  this.update(paymentInfo,wrapper);

        //  利用mq 发送消息：到底是充值业务，还是支付订单业务。
        String topic = paymentInfo.getPaymentType().equals(SystemConstant.PAYMENT_TYPE_ORDER)? KafkaConstant.QUEUE_ORDER_PAY_SUCCESS:KafkaConstant.QUEUE_RECHARGE_PAY_SUCCESS;
        //  outTradeNo:微信系统对应的商户订单号.order_info  paymentInfo.getOrderNo(); recharge_info
        //  修改订单状态。或修改充值状态.
        kafkaService.sendMsg(topic,paymentInfo.getOrderNo());

    }

    @Override
    public void savePaymentInfo(OrderInfo orderInfo, String paymentType) {
        //  添加数据：
        //  创建对象
        PaymentInfo paymentInfo = new PaymentInfo();
        //  赋值：
        paymentInfo.setUserId(orderInfo.getUserId());
        paymentInfo.setPaymentType(paymentType);
        paymentInfo.setOrderNo(orderInfo.getOrderNo());
        paymentInfo.setPayWay(orderInfo.getPayWay());
        //  只有订单的金额：
        //  paymentInfo.setAmount(orderInfo.getOrderAmount());
        if (paymentType.equals(SystemConstant.PAYMENT_TYPE_RECHARGE)){
            //  获取充值记录
            Result<RechargeInfo> rechargeInfoResult = rechargeInfoFeignClient.getRechargeInfo(orderInfo.getOrderNo());
            Assert.notNull(rechargeInfoResult,"充值结果集为空");
            RechargeInfo rechargeInfo = rechargeInfoResult.getData();
            Assert.notNull(rechargeInfo,"充值对象为空");
            paymentInfo.setAmount(rechargeInfo.getRechargeAmount());
        } else {
            //  赋值交易记录金额
            paymentInfo.setAmount(orderInfo.getOrderAmount());
        }

        //  微信或支付宝的交易号
        //  paymentInfo.setOutTradeNo();
        String content = paymentType.equals(SystemConstant.PAYMENT_TYPE_ORDER)?"订单支付":"充值支付";
        paymentInfo.setContent(content);
        paymentInfo.setPaymentStatus(SystemConstant.PAYMENT_STATUS_UNPAID);
        //  保存交易记录.
        this.save(paymentInfo);
    }
}
